// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.

// Adapted from http://rastertek.com/dx10tut39.html

/////////////
// GLOBALS //
/////////////
cbuffer MatrixBuffer
{
    matrix worldMatrix;
    matrix viewMatrix;
    matrix projectionMatrix;
    float3 cameraPos;
    float pad; // byte width must be a multiple of 16
};

//////////////
// TYPEDEFS //
//////////////

//The two input types both have a color component so that the particle can have an individual color that is added to the texture base color. 
struct VertexInputType
{
    float3 position : POSITION;
    float2 tex : TEXCOORD0;
    float4 color : COLOR;
};

struct PixelInputType
{
    float4 position : SV_POSITION;
    float2 tex : TEXCOORD0;
    float4 color : COLOR;
};


////////////////////////////////////////////////////////////////////////////////
// Vertex Shader
////////////////////////////////////////////////////////////////////////////////
PixelInputType ParticleVertexShader(VertexInputType input)
{
    PixelInputType output;

    float3 xAllowedRotDir = float3(0,1,0); // Assume up is +y, could parameterize

    // Change the position vector to be 4 units for proper matrix calculations.
    float4 pos = float4(input.position, 1.0f);

    float3 center = mul(pos, worldMatrix).xyz;
    float3 eyeVector = center - cameraPos;

    float3 upVector = xAllowedRotDir;
    upVector = normalize(upVector);
    float3 sideVector = cross(eyeVector,upVector);
    sideVector = normalize(sideVector);

    float3 finalPosition = center;

    float4 finalPosition4 = float4(finalPosition, 1);

    float4x4 preViewProjection = mul (viewMatrix, projectionMatrix);
    output.position = mul(finalPosition4, preViewProjection);

    // Store the texture coordinates for the pixel shader.
    output.tex = input.tex;

    // The color is sent through to the pixel shader here. 
    // Store the particle color for the pixel shader. 
    output.color = input.color;

    return output;
}